package club.codeworker.commonscodecdemo.controller;

import club.codeworker.commonscodecdemo.config.MyPasswordEncoder;
import club.codeworker.commonscodecdemo.entity.User;
import club.codeworker.commonscodecdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-09-05 21:13:50
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Autowired
    private MyPasswordEncoder encoder;

    /**
     * 用户注册
     *
     * @param user 主键
     * @return 单条数据
     */
    @PostMapping("/register")
    public User register(User user) {
        //密码加密
        user.setPassword(encoder.encode(user.getPassword()));
        return this.userService.insert(user);
    }

    @PostMapping("/login")
    public boolean login(User user) {
        String password = this.userService.queryByUniqueName(user.getName()).getPassword();
        return encoder.matches(user.getPassword(), password);
    }

}
